package com.kira.scaffoldmvc.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kira.scaffoldmvc.Mapper.UserMapper;
import com.kira.scaffoldmvc.POJO.DTO.TokenDTO;
import com.kira.scaffoldmvc.POJO.PageBean;
import com.kira.scaffoldmvc.POJO.RentBook;
import com.kira.scaffoldmvc.POJO.Result;
import com.kira.scaffoldmvc.POJO.User;
import com.kira.scaffoldmvc.Service.UserService;
import com.kira.scaffoldmvc.Utils.JwtUtil;
import com.kira.scaffoldmvc.Utils.Md5Util;
import com.kira.scaffoldmvc.Utils.RandomGenerator;
import com.kira.scaffoldmvc.Utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result lock(String userId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String myUserId = (String) claims.get("userId");
        Integer myRight = userMapper.selectRight(myUserId);
        if (myRight == 1 || myRight == 2) {

        } else
            return Result.error("该用户不是管理员用户");

        userMapper.lock(userId);

        return Result.success("用户封禁成功");
    }

    @Override
    public Result unlock(String userId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String myUserId = (String) claims.get("userId");
        Integer myRight = userMapper.selectRight(myUserId);
        if (myRight == 1 || myRight == 2) {

        } else
            return Result.error("该用户不是管理员用户");

        userMapper.unlock(userId);
        return Result.success("用户解锁成功");
    }

    @Override
    public Result setManage(Integer right, String userId) {

        Map<String, Object> claims = ThreadLocalUtil.get();
        String myUserId = (String) claims.get("userId");
        Integer myRight = userMapper.selectRight(myUserId);
        if (myRight == 1 || myRight == 2) {

        } else
            return Result.error("该用户不是管理员用户");

        userMapper.setRight(right, userId);
        return Result.success("修改成功");

    }


    @Override
    public Integer registry(String name, String password, String account) {

        User user = userMapper.selectByAccount(account);
        if (user != null)
            return 1;//账号重复

        password = Md5Util.getMD5String(password);
        String userId = RandomGenerator.generateRandom(6);
        userMapper.registry(name, password, account, userId);
        return 0;

    }

    @Override
    public Result<PageBean<User>> getAllUser(Integer pageNum, Integer pageSize) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String myUserId = (String) claims.get("userId");
        Integer myRight = userMapper.selectRight(myUserId);
        if (myRight == 1 || myRight == 2) {

        } else
            return Result.error("该用户不是管理员用户");

        PageBean<User> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);

        List<User> rentBooks = userMapper.getAllUser();


        PageInfo<User> page = new PageInfo<>(rentBooks);
        pb.setTotal(page.getTotal());
        pb.setItems(page.getList());

        return Result.success(pb);


    }

    @Override
    public Result login(String account, String password) {

        User user = userMapper.selectByAccount(account);
        if (user == null)
            return Result.success("账号错误");

        boolean b = Md5Util.checkPassword(password, user.getPassword());
        if (b == false)
            return Result.error("密码错误");

        Map<String, Object> claims = new HashMap<>();
        claims.put("name", user.getName());
        claims.put("userId", user.getUserId());
        claims.put("ban", user.getBan());
        claims.put("right", user.getRight());
        claims.put("account", user.getAccount());
        String token = JwtUtil.genToken(claims);
        TokenDTO dto = new TokenDTO();
        dto.setToken(token);
        dto.setRight(user.getRight());
        dto.setUserId(user.getUserId());
        return Result.success(dto);
    }


    @Override
    public Result getUser(String userId) {
        return Result.success(userMapper.getUserById(userId));
    }
}
